package com.open.thinks.todo.system.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.open.thinks.todo.system.common.WebUtils;
import com.open.thinks.todo.system.domain.User;
import com.open.thinks.todo.system.service.UserService;

@Controller
@RequestMapping
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping("/login")
	public String login(User user) {
		return "login";
	}

	@RequestMapping("/login/process")
	public String login_process(@Valid User user, BindingResult result, HttpSession session) {
		if (result.hasErrors()) {
			return login(user);
		}
		User validateUser = userService.validate(user);
		if (validateUser != null) {
			session.setAttribute(WebUtils.SESSION_LOGIN, validateUser);
			return "redirect:/task/list" + WebUtils.ACCESS_SUFFIX;
		}
		return login(user);
	}
}
